package com.example.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.api.entity.DatabaseInfo;
import com.example.api.service.impl.DataBaseInfoServiceImpl;

@RequestMapping("/databaseHandler")
@RestController
public class DatabaseInfoController {

	@Autowired
	private DataBaseInfoServiceImpl dataBaseInfoService;
	
	
	/**
	 * 获取数据库连接下拉列表
	 * @param userId
	 * @return
	 */
	@GetMapping("getDatabaseInfoList")
	public List<DatabaseInfo> getDatabaseInfoList(Integer userId) {
		List<DatabaseInfo> list = dataBaseInfoService.getDatabaseInfoByuserId(1);
		return list;
	}
	
	/**
	 * 获取左侧数据库名称列表
	 * @param databaseConnectionId
	 * @return
	 */
	@GetMapping("getDatabaseInfoById")
	public Map<String, Object> getDatabaseInfoById(Integer databaseConnectionId) {
		List<Map<String, String>> databaseInfo = dataBaseInfoService.getDatabaseInfoByConnectionId(databaseConnectionId);
		Map<String, Object>map=new HashMap<>();
		map.put("total", 2);
		map.put("rows", databaseInfo);
		map.put("footer", new ArrayList<>());
		return map;
	}
	/**
	 * 获取数据库配置信息
	 * @param userId
	 * @return
	 */
	@GetMapping("getDatabaseConfigList")
	public List<DatabaseInfo> getDatabaseConfigInfoByuserId(Integer userId){
		List<DatabaseInfo> list=dataBaseInfoService.getDatabaseConfigInfoByuserId(userId);
		return list;
	}
	
	/**
	 * 获取数据库表的名称列表
	 * @param databaseInfo
	 * @param connectionId
	 * @return
	 */
	@PostMapping("getTableInfoBydatabaseInfo")
	public List<Map<String, Object>> getTableInfoBydatabaseInfo(String databaseName,Integer connectionId){
		List<Map<String, Object>> result = dataBaseInfoService.getTableInfoBydatabaseInfo(databaseName, connectionId);
		return result;
	}
	
	/**
	 * 获取视图名称列表
	 * @param databaseName
	 * @param connectionId
	 * @return
	 */
	@PostMapping("/getViewListBydatabaseInfo")
	public List<Map<String, Object>>getViewListBydatabaseInfo(String databaseName, Integer connectionId){
		List<Map<String, Object>> result = dataBaseInfoService.getViewListBydatabaseInfo(databaseName, connectionId);
		return result;
	}
	
	/**
	 * 获取数据表的字段信息
	 * @param databaseName
	 * @param connectionId
	 * @param tableName
	 * @return
	 */
	@PostMapping("getTableFieldList")
	private Map<String, Object> getTableFieldList(String databaseName, Integer connectionId,String tableName) {
		Map<String, Object> tableFieldList=dataBaseInfoService.getTableFieldList(databaseName, connectionId, tableName);
		return tableFieldList;
	} 
	
	/**
	 * 获取数据库视图的字段信息
	 * @param databaseName
	 * @param connectionId
	 * @param tableName
	 * @return
	 */
	@PostMapping("getViewFieldList")
	private Map<String, Object> getViewFieldList(String databaseName, Integer connectionId,String viewName) {
		Map<String, Object> viewFieldList=dataBaseInfoService.getViewFieldList(databaseName, connectionId, viewName);
		return viewFieldList;
	}
	
	/**
	 * 添加数据库连接信息
	 * @param databaseInfo
	 * @return
	 */
	@PostMapping("/addDatabaseConfigInfo")
	public Map<String,Object> addDatabaseConfigInfo(@RequestBody DatabaseInfo databaseInfo,HttpServletRequest request) {
		int count;
		Map<String,Object>map = new HashMap<>();
		Integer userId=(Integer) request.getSession().getAttribute("userId");
		databaseInfo.setUserId(userId);
		try {
			count = dataBaseInfoService.addDatabaseConfigInfo(databaseInfo);
			map.put("state", 200);
			map.put("message", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("state",0);
			map.put("message", "添加失败");
		}
		return map;
	}
	
	@PostMapping("deleteDatabaseConfigInfo")
	public Map<String, Object>deleteDatabaseConfigInfo(Integer connectionId){
		Map<String,Object>map = new HashMap<>();
		try {
			int count = dataBaseInfoService.deleteDatabaseConfigInfo(connectionId);
			map.put("state", 200);
			map.put("message", "删除成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("state",0);
			map.put("message", "删除失败");
		}
		return map;
	}
	
}
